package com.project.common.utils;


import lombok.AllArgsConstructor;
import lombok.Data;

/**
* @Description:   请求Header临时存储
* @Author:         wangzhiqiang
* @CreateDate:     2019/1/18 16:16
* @Version:        1.0
*/
public class ThreadLocalBuilder {

    /**
     * 本地线程池
     */
    private static ThreadLocal<ThreadArg> threadArgThreadLocal = new ThreadLocal<>();

    /**
     * 写入线程
     * @param token
     */
    public static void setThreadArg(String token){
        threadArgThreadLocal.set(new ThreadArg(token));
    }

    /**
     * 获取线程
     * @return
     */
    public static ThreadArg getThreadArg(){
        return threadArgThreadLocal.get();
    }

    /**
     * 获取token
     * @return
     */
    public static String getToken(){
        ThreadArg threadArg = getThreadArg();
        if(null != threadArg){
            return threadArg.getToken();
        }
        return null;
    }

    /**
     * 清空线程池
     */
    public static void delThreadArg(){
        threadArgThreadLocal.remove();
    }

    @Data
    @AllArgsConstructor
    public static class ThreadArg {

        /**
         * 登录token
         */
        private String token;
    }
}
